var tableCategory = null ;
var tableDataType = null ;
var tableCompletion = null ;

function refreshTable(){
    generateFullTable({
        dataToUse: tableDataType
    });
}

/** Creates a google visualization table from the data loaded.
 *  Assumes that the google visualization api is preloaded.
 * @param {Object} givenOptions The options, see the defaults in the code. The dataToUse param can be 'absolute', 'relative' or 'section'
 */
var currentData = null;
function generateFullTable(givenOptions){
    var defaults = {
        dataToUse: 'absolute',
        targetDiv: 'fulltable',
        googleDataOptions: {
            showRowNumber: false
        }
    };
    var options = {};
    jQuery.extend(options, defaults, givenOptions);
    
    if (stats == null) {
        stats = calculateStatistics();
    }
    
    var data = new google.visualization.DataTable();
    
    //Fill columns
    data.addColumn('string', 'Rajtszám');
    data.addColumn('string', 'Név');
    for (var i = 0; i < checkPoints.length; ++i) {
        data.addColumn('string', checkPoints[i].name);
    }
    
    var competitorCount = competitors.length;//stats.competitorCount;
    //data.addRows(competitorCount);
    data.addRows(competitorCount) ;
    //Fill competitor names and ids
    var cct = 0;
    for (var i = 0; i < competitorCount; ++i) {
        if ( (tableCategory == competitors[i].category || tableCategory == competitors[i].originalDistance) && 
                       (    (tableCompletion == "N")  
                         || (tableCategory == competitors[i].category && tableCompletion == "T" && competitors[i].finished() ) 
                         || (tableCategory == competitors[i].category && tableCompletion == "SZ"&& competitors[i].finishedWithinTimeLimit() ) ) ) {
            data.setCell(cct, 0, competitors[i].id);
            data.setCell(cct, 1, competitors[i].name);
            cct++ ;            
        }
    }
    
    var i, j, value;
    for (var i = 0; i < checkPoints.length; ++i) {
        var measurements = checkPoints[i].measurementsByCompetitor;
        if (options.dataToUse == 'relative') {
            measurements = checkPoints[i].relativeTimesByCompetitor;
        } else if (options.dataToUse == 'section') {
            measurements = checkPoints[i].sectionTimesByCompetitor;
        }
        cct = 0;
        for (j = 0; j < competitorCount; ++j) {
            if ( (tableCategory == competitors[j].category  || tableCategory == competitors[j].originalDistance) && 
                       (    (tableCompletion == "N")  
                         || (tableCategory == competitors[j].category  && tableCompletion == "T" && competitors[j].finished() ) 
                         || (tableCategory == competitors[j].category  && tableCompletion == "SZ"&& competitors[j].finishedWithinTimeLimit() ) ) ) {
                try {
                    value = measurements[competitors[j].id];
                    if (!((typeof value) === 'undefined')) {
                        var minutes = (value.getMinutes() < 10 ? '0' : '') + value.getMinutes();
                        var formattedValue = value.getHours() + ':' + minutes;
                        if (options.dataToUse == 'relative' && value.getTime() > 23*60*60*1000 ) {
                            formattedValue = 24 + value.getHours() + ":" + minutes;
                        }
                        data.setCell(cct, i + 2, formattedValue);
                    } else {
                        // cell remains empty due to missing data
                    }
                } catch (e) {
                    data.setCell(cct, i + 2, "-");
                    //alert(err) ;
                }
                cct++;
            }
        }
    }
    
    var table = new google.visualization.Table(document.getElementById(options.targetDiv));
    currentData = data;
    table.draw(data, options.googleDataOptions);
}



